package novel.mzx.com.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import novel.mzx.com.R;
import novel.mzx.com.base.BaseActivity;
import novel.mzx.com.bean.CodeBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.http.SpDataUtils;
import novel.mzx.com.utils.JSONUtils;
import novel.mzx.com.utils.SPUtil;
import novel.mzx.com.utils.StringUtils;
import novel.mzx.com.utils.ToastUtils;
import novel.mzx.com.views.XCRoundImageView;

/**
 * 修改个人信息页面：
 * */

public class EditUserActivity extends BaseActivity {

    @BindView(R.id.btn_login)
    Button btn_login;

    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.iv_logo)
    XCRoundImageView iv_logo;

    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password2)
    EditText et_password2;

    @BindView(R.id.iv_back)
    ImageView iv_back;
    EditText et_nickName;
    @BindView(R.id.et_age)
    EditText et_age;
    EditText et_hobby;
    @BindView(R.id.cb_01)
    CheckBox cb_01;
    @BindView(R.id.cb_02)
    CheckBox cb_02;

    String sex = "男";

    @Override
    protected int getResourceId() {
        return R.layout.activity_edit_user;
    }

    // 初始化控件
    @Override
    protected void initView() {
        super.initView();
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init();
        ImmersionBar.with(EditUserActivity.this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();

        Glide.with(this).load(Api.mainImgHost+ SpDataUtils.getUserLogo());

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        cb_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_01.setChecked(true);
                cb_02.setChecked(false);
                sex = "男";
            }
        });

        cb_02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_01.setChecked(false);
                cb_02.setChecked(true);
                sex = "女";
            }
        });



        // 提交修改信息按钮的基本逻辑处理
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (StringUtils.getEditTextData(etPhoneNum).isEmpty() && StringUtils.getEditTextData(etPassword).isEmpty()){
                    ToastUtils.showToast(EditUserActivity.this,"请输入用户名或密码");
                    return;
                }
                if(StringUtils.getEditTextData(etPhoneNum).isEmpty()){
                    ToastUtils.showToast(EditUserActivity.this,"请输入用户名");
                    return;
                }

                if(StringUtils.getEditTextData(etPassword).isEmpty()){
                    ToastUtils.showToast(EditUserActivity.this,"请输入密码");
                    return;
                }

                if(StringUtils.getEditTextData(et_password2).isEmpty()){
                    ToastUtils.showToast(EditUserActivity.this,"请输入密码");
                    return;
                }

                if(!StringUtils.getEditTextData(etPassword).equals(StringUtils.getEditTextData(et_password2))){
                    ToastUtils.showToast(EditUserActivity.this,"两次输入的密码不一直");
                    return;
                }

                // 获取
                String phone = StringUtils.getEditTextData(etPhoneNum);
                String pwd = StringUtils.getEditTextData(etPassword);
                try {
                    // 基本信息自动传入
                    String memberId = SPUtil.getInstance().getString("memberId");
                    OkGo.<String>post(Api.mainHost+Api.updateUserUrl)
                            .params("userId",memberId)
                            .params("userPwd",pwd)
                            .params("userName",phone)
                            .params("nickName",et_nickName.getText().toString().trim())
                            .params("sex",sex)
                            .params("age",et_age.getText().toString().trim())
                            .params("hobby",et_hobby.getText().toString().trim())
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    // 调用接口修改数据库文件
                                    Log.e("Registration",response.body());
                                    CodeBean codeBean = JSONUtils.parserObject(response.body(),CodeBean.class);
                                    int code = codeBean.getResultCode();
                                    if(code == 200){
                                        ToastUtils.showToast(EditUserActivity.this,"修改成功!");
                                        finish();
                                    }else {
                                        ToastUtils.showToast(EditUserActivity.this,"修改失败!");
                                    }
                                }
                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);

                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });






    }



}
